package org.sam.swing.table.editor;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;

import javax.swing.AbstractCellEditor;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;

/**
 * 自定义表达式的editor对象
 * 
 * @author sam
 *
 */
public class JSTableEmptyExpressionEditor extends AbstractCellEditor implements TableCellEditor, TreeCellEditor {

	private static final long serialVersionUID = 8072214447446501351L;

	/**
	 * 操作代理对象
	 */
	private EditorDelegate delegate;

	/**
	 * 表达式对象
	 */
	private Expression el;

	/**
	 * 用户选择对象
	 */
	private JLabel editorComponent;

	/**
	 * 初始化对象显示
	 * 
	 * @param el
	 */
	public JSTableEmptyExpressionEditor(Expression el) {
		this.editorComponent = new JLabel("");
		delegate = new EditorDelegate();
		delegate.setClickCountToStart(1);
		this.el = el;

	}

	/**
	 * 获取值
	 * 
	 * @return
	 */
	@Override
	public Object getCellEditorValue() {
		return delegate.getCellEditorValue();
	}

	/**
	 * 树类型的控件
	 */
	@Override
	public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
			boolean leaf, int row) {
		return null;
	}

	/**
	 * 表格类型的控件
	 */
	@Override
	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		if (isSelected) {
			editorComponent.setBackground(table.getSelectionBackground());
		} else {
			editorComponent.setBackground(table.getBackground());
		}

		if (this.el != null) {
			Object v = this.el.getValue(table, value, isSelected, row, column);
			editorComponent.setText(el.getText(table, v, isSelected, row, column));
			delegate.setValue(v);
		} else {
			delegate.setValue(value);
		}

		return editorComponent;
	}

	/**
	 * 获取值的回调接口
	 * 
	 * @author sam
	 *
	 */
	public interface Expression {

		/**
		 * 回调接口
		 * 
		 * @param table
		 *            表格
		 * @param value
		 *            值
		 * @param isSelected
		 *            是否选中
		 * @param row
		 *            行
		 * @param column
		 *            列
		 * @return
		 */
		public Object getValue(JTable table, Object value, boolean isSelected, int row, int column);

		/**
		 * 
		 * @param table
		 * @param value
		 * @param isSelected
		 * @param row
		 * @param column
		 * @return
		 */
		public String getText(JTable table, Object value, boolean isSelected, int row, int column);
	}

	/**
	 * 实现的内部方法
	 * 
	 * @author sam
	 *
	 */
	protected class EditorDelegate extends JSEditorDelegateAdapter {

		private static final long serialVersionUID = -9163652875049976071L;

		/**
		 * 值发生改变的时候执行的操作
		 */
		public void setValue(Object value) {
			super.setValue(value);
		}

		/**
		 * Stops editing and returns true to indicate that editing has stopped.
		 * This method calls <code>fireEditingStopped</code>.
		 *
		 * @return true
		 */
		public boolean stopCellEditing() {
			fireEditingStopped();
			return true;
		}

		/**
		 * Cancels editing. This method calls <code>fireEditingCanceled</code>.
		 */
		@Override
		public void cancelCellEditing() {
			fireEditingCanceled();
		}

		/**
		 * When an action is performed, editing is ended.
		 * 
		 * @param e
		 *            the action event
		 * @see #stopCellEditing
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			JSTableEmptyExpressionEditor.this.stopCellEditing();
		}

		/**
		 * When an item's state changes, editing is ended.
		 * 
		 * @param e
		 *            the action event
		 * @see #stopCellEditing
		 */
		@Override
		public void itemStateChanged(ItemEvent e) {
			JSTableEmptyExpressionEditor.this.stopCellEditing();
		}
	}

}
